<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2017/9/29
 * Time: 19:08
 */

namespace app\librarys;


class MobilePhoneAreaQuery
{
//    private static $arr = array();
   public  function query($phone)
   {
       $cache = \Yii::$app->cache;
       $area = $cache->get('area'.$phone);
       if($area != null || $area != false)
       {
           return $area;
       }

       $host = "http://jisushouji.market.alicloudapi.com";
       $path = "/shouji/query";
       $method = "GET";
       $appcode = "37b9fc55db4643eb952ad70fc45d3cfe";
       $headers = array();
       array_push($headers, "Authorization:APPCODE " . $appcode);
       $querys = "shouji=$phone";
       $bodys = "";
       $url = $host . $path . "?" . $querys;

       $curl = curl_init();
       curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
       curl_setopt($curl, CURLOPT_URL, $url);
       curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($curl, CURLOPT_FAILONERROR, false);
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_HEADER, false);
       if (1 == strpos("$".$host, "https://"))
       {
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
       }
//       var_dump(curl_exec($curl));
       $result = curl_exec($curl);
       $item = json_decode($result);
       if($item != null)
       var_dump($item);
       if($item == null || $item->status != 0)
           return -1;
//       var_dump($item->result->province);
//       self::$arr["$phone"]=$item->result->province;
//       var_dump(self::$arr);

       $cache->set('area'.$phone, $item->result->province, 0);
       return $item->result->province;
   }
}